/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.attendance.clazz.core;

import com.arisgo.cloud.attendance.clazz.vo.TemporaryClassVo;
import com.arisgo.cloud.common.utils.AssertUtil;
import com.arisgo.cloud.common.utils.DateUtil;
import com.arisgo.cloud.core.Context;
import com.arisgo.cloud.web.utils.RequestUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

/**
 * 排班上下文
 *
 * @author Coulson
 * @since 2025/3/25
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ArrangeContext extends Context {
    /**
     * 租户id
     */
    private Long tenantId;
    /**
     * 班组
     */
    private Long groupId;
    /**
     * 排班开始时间
     */
    private LocalDate startDate;
    /**
     * 排班结束时间
     */
    private LocalDate endDate;
    /**
     * 排班日（工作日）
     */
    private List<LocalDate> workdays;
    /**
     * 临时班
     */
    private TemporaryClassVo temporaryClass;
    /**
     * 员工
     */
    private List<Long> staffIds;
    /**
     * 排班策略
     */
    private String strategy;

    public LocalDate getStartDate() {
        if (null == startDate) {
            startDate = DateUtil.weekFirstDay(LocalDate.now(), 1);
        }
        return startDate;
    }

    // 默认排下周班（工作日）
    public LocalDate getEndDate() {
        if (null == endDate) {
            endDate = DateUtil.weekLastDay(getStartDate());
        }
        AssertUtil.isTrue(this.endDate.isAfter(getStartDate()), "排班结束时间必须大于等于排班开始时间！");
        return endDate;
    }

    public Long getTenantId() {
        if (null == tenantId) {
            this.tenantId = RequestUtil.getTenantId();
        }
        return tenantId;
    }

    public List<LocalDate> getWorkdays() {
        if (null == this.workdays) {
            return Collections.emptyList();
        }
        return this.workdays;
    }

    public List<Long> getStaffIds() {
        if (null == this.staffIds) {
            return Collections.emptyList();
        }
        return this.staffIds;
    }

    @Override
    public ArrangeContext clone() {
        try {
            return (ArrangeContext) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }

}
